package com.example.secondhandshop.model;

import android.os.NetworkOnMainThreadException;
import android.util.Log;

import com.example.secondhandshop.bean.AllchatitemData;
import com.example.secondhandshop.bean.Allchatuser;
import com.example.secondhandshop.bean.Chatitem;
import com.example.secondhandshop.bean.ResponseBody;
import com.example.secondhandshop.ui.activity.Constans;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ChatuserModel {
    OkHttpClient client = new OkHttpClient();
    public void getallchatuser(int userid, ChatuserModel.OnProductsFetchedListener listener) {
        new Thread(() -> {
            // url路径
            String url = Constans.SERVER_URL()+"tran/chat/user?userId="+userid;
            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", Constans.getAppId())
                    .add("appSecret",Constans.getAppSecret())
                    .build();
            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .get()
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        listener.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.e("ChatuserModel", "onResponse: " + response);
                        if (response.isSuccessful()) {
                            String ChatuserResponseBody = response.body().string();
                            Log.d("ChatuserModel(1)", "API Response Body: " + ChatuserResponseBody );
                            Gson gson = new Gson();
                            ResponseBody<List<Allchatuser>> responseBody = gson.fromJson(ChatuserResponseBody, new TypeToken<ResponseBody<List<Allchatuser>>>() {
                            }.getType());
                            // 检查响应代码
                            if (responseBody.getCode() == 200) {
                                List<Allchatuser> allchatusers= responseBody.getData();
                                System.out.println(allchatusers.toString());
                                listener.onSuccess(allchatusers);
                            } else {
                                listener.onFailure(responseBody.getMsg()); // 使用实际消息
                            }
                        } else {
                            listener.onFailure(response.message()); // 捕获请求失败的情况
                        }
                    }
                });
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
    }

    public interface  OnProductsFetchedListener {
        void onFailure(String message);
        void onSuccess(List<Allchatuser> allchatusers);
    }
}
